package com.siwei.apply.service.impl;

import com.siwei.apply.domain.Project;
import com.siwei.apply.domain.Ydbp;
import com.siwei.apply.domain.res.YdbpRes;
import com.siwei.apply.domain.vo.YdbpUpdateVo;
import com.siwei.apply.domain.vo.YdbpVo;
import com.siwei.apply.mapper.ProjectMapper;
import com.siwei.apply.mapper.YdbpMapper;
import com.siwei.apply.service.YdbpService;
import com.siwei.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.siwei.apply.domain.ProjectWorkflow;
import com.siwei.apply.domain.Workflow;
import com.siwei.apply.mapper.ProjectWorkflowMapper;
import com.siwei.apply.mapper.WorkflowMapper;

import java.util.List;

import static com.siwei.apply.common.Common.UserId;

/**
 * 用地报批 服务实现类
 */
@Service
public class YdbpImpl implements YdbpService {
    @Autowired
    private YdbpMapper ydbpMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectWorkflowMapper projectWorkflowMapper;
    @Autowired
    private WorkflowMapper workflowMapper;

    @Override
    public Boolean isExit(String projectId) {
        return ydbpMapper.isExit(projectId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String add(YdbpVo ydbpVo) {
        // 添加
        Ydbp ydbp = new Ydbp();
        BeanUtils.copyProperties(ydbpVo, ydbp);
        ydbp.generateId();
        ydbp.setCreatorId(UserId);
        ydbpMapper.add(ydbp);
        // 添加项目流程

        /*1.获取项目类型*/
        Integer projectType = projectMapper.getProjectTypeById(ydbp.getProjectId());
        /*2.获取当前位置工作流*/
        Workflow workflow = workflowMapper.selectByProjectTypeAndTableName(projectType, "t_ydbp");
        /*3.获取上一个流程工作流*/
        Workflow workflowPrev = workflowMapper.selectPrevByProjectTypeAndTableName(projectType, "t_ydbp");
        /*4.获取上一个工作流的节点信息*/
        List<ProjectWorkflow> projectWorkflows = projectWorkflowMapper.selectByProjectIdAndNodeTableName(ydbp.getProjectId(), workflowPrev.getTableName());
        String parentId = "";
        if (projectWorkflows != null && !projectWorkflows.isEmpty()) {
            // TODO: 如果有多个上一个工作流节点，应该如何处理？目前取第一个
            parentId = projectWorkflows.get(0).getNodeId();
        }
        ProjectWorkflow projectWorkflow = new ProjectWorkflow();
        projectWorkflow.setId(ydbp.getId());
        projectWorkflow.setParentId(parentId);
        projectWorkflow.setWorkflowId(workflow.getId());
        projectWorkflow.setProjectId(ydbp.getProjectId());
        projectWorkflow.setNodeId(ydbp.getId());
        projectWorkflow.setNodeTableName(workflow.getTableName());
        projectWorkflow.setWorkflowName(workflow.getName());
        projectWorkflowMapper.add(projectWorkflow);
        return ydbp.getId();
    }

    @Override
    public YdbpRes get(String projectId) {
        Ydbp ydbp = ydbpMapper.get(projectId);
        Project project = projectMapper.get(projectId);

        YdbpRes ydbpRes = new YdbpRes();
        if (ydbp != null) {
            BeanUtils.copyProperties(ydbp, ydbpRes);
        }
        ydbpRes.setProjectId(projectId);
        ydbpRes.setProjectName(project.getName());
        ydbpRes.setProjectCode(project.getCode());
        ydbpRes.setProjectType(project.getProjectType());
        return ydbpRes;
    }

    @Override
    public YdbpRes getById(String id) {
        Ydbp ydbp = ydbpMapper.getById(id);
        if (ydbp == null) {
            return null;
        }
        Project project = projectMapper.get(ydbp.getProjectId());
        YdbpRes ydbpRes = new YdbpRes();
        BeanUtils.copyProperties(ydbp, ydbpRes);
        ydbpRes.setProjectId(ydbp.getProjectId());
        ydbpRes.setProjectName(project.getName());
        ydbpRes.setProjectCode(project.getCode());
        ydbpRes.setProjectType(project.getProjectType());
        return ydbpRes;
    }

    @Override
    public void update(YdbpUpdateVo ydbpUpdateVo) {
        ydbpMapper.update(ydbpUpdateVo);
    }

    @Override
    public void updateHasOnchain(String id, Boolean hasOnchain) {
        ydbpMapper.updateHasOnchain(id, hasOnchain);
    }
}
